package com.guofen.service.cylj.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guofen.dao.KitchenwasteDischargeMapper;
import com.guofen.pojo.KitchenwasteDischarge;
import com.guofen.service.cylj.KitchenwasteDischargeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class KitchenwasteDischargeimpl implements KitchenwasteDischargeService {
    @Autowired
    private KitchenwasteDischargeMapper kitchenwasteDischargeMapper;
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean submitAllInfo(KitchenwasteDischarge kitchenwasteDischarge) {
        int flag = kitchenwasteDischargeMapper.insert(kitchenwasteDischarge);
        if (flag > 0){
            return true;
        }else {
            return false;
        }

    }

    @Override
    public KitchenwasteDischarge getlast() {
        return kitchenwasteDischargeMapper.getLast();
    }

    @Override
    public Map<String, Object> get(int pageSize, int currentPage) {
        Map<String, Object> map = new HashMap<>();
        PageHelper.startPage(currentPage, pageSize);
        List<KitchenwasteDischarge> list = kitchenwasteDischargeMapper.selectAll();
        PageInfo pageRecord = new PageInfo(list);
        long sum = pageRecord.getTotal();
        list = pageRecord.getList();
        map.put("records",list);
        map.put("sum",sum);
        return map;
    }

    @Override
    public Map<String, Object> getByTime(int pageSize, int currentPage, long startTime, long endTime) {
        Map<String, Object> map = new HashMap<>();
        PageHelper.startPage(currentPage, pageSize);
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String startDate = dateFormat.format(new Date(startTime));
        String endDate = dateFormat.format(new Date(endTime));
        List<KitchenwasteDischarge> list = kitchenwasteDischargeMapper.selectByDate(startDate, endDate);
        PageInfo pageRecord = new PageInfo(list);
        long sum = pageRecord.getTotal();
        list = pageRecord.getList();
        map.put("records",list);
        map.put("sum",sum);
        return map;
    }

    @Override
    public int delete(int id) {
        return kitchenwasteDischargeMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int update(KitchenwasteDischarge record) {
        return kitchenwasteDischargeMapper.updateByPrimaryKeySelective(record);
    }

}
